package com.tth.learn.addproperties.exer;

import org.junit.Test;

import java.util.*;

/**
 * @Author tth
 * @Date 2022/12/14 16:46
 */
public class CollectionTest {
    @Test
    public void test1() {
        List list = new ArrayList();
        for (int i = 0; i < 10; i++) {
            double random = Math.random();
            list.add((int)(random * 100));
        }

        System.out.println("排序前："+list);
        Collections.sort(list, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Integer && o2 instanceof Integer) {
                    Integer i1 = (Integer) o1;
                    Integer i2 = (Integer) o2;
                    return -Integer.compare(i1, i2);
                }
                throw new RuntimeException("输入的数据格式不匹配");
            }
        });
        System.out.println("排序后："+list);
    }

    @Test
    public void test2() {
        TreeSet treeSet = new TreeSet();
        treeSet.add(new Student(1001, "张1", 89));
        treeSet.add(new Student(1003, "张2", 54));
        treeSet.add(new Student(1002, "张3", 59));
        treeSet.add(new Student(1008, "张4", 75));
        treeSet.add(new Student(1005, "张5", 88));
        treeSet.add(new Student(1007, "张6", 92));
        treeSet.add(new Student(1004, "张7", 89));
        treeSet.add(new Student(1009, "张8", 90));

        int count = 0;
        Iterator iterator = treeSet.iterator();
        while (iterator.hasNext() && count < 3) {
            System.out.println(iterator.next());
            count++;
        }

    }
}
class Student implements Comparable{
    private Integer id;
    private String name;
    private int score;

    public Student(Integer id, String name, int score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return score == student.score &&
                Objects.equals(id, student.id) &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, score);
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Student) {
            Student s = (Student) o;
            int compare = -Integer.compare(this.score, s.score);
            if (compare == 0) {
                compare = Integer.compare(this.id, s.id);
                if (compare == 0) {
                    compare = this.name.compareTo(s.name);
                }
            }
            return compare;
        }
        throw new RuntimeException("数据格式不匹配");
    }
}
